//
//  NSArray+RAC.m
//  高仿暴走斗图
//
//  Created by key on 16/9/9.
//  Copyright © 2016年 huangsongyao. All rights reserved.
//

#import "NSArray+RAC.h"

@implementation NSArray (RAC)

- (RACSequence *)rac_traverseFilerBlock:(BOOL(^)(id x))filer mapBlock:(id(^)(id x))map
{
    RACSequence *results = [[self.rac_sequence filter:^BOOL(id value) {
        if (filer) {
            return filer(value);
        }
        return NO;
    }] map:^id(id value) {
        if (map) {
            return map(value);
        }
        return nil;
    }];
    return results;
}

- (RACSignal *)rac_traverseArray {
    
    if (self.count == 0) {
        return nil;
    }
    static NSInteger index = -1;
    
    @weakify(self);
    return [RACSignal createSignal:^RACDisposable *(id<RACSubscriber> subscriber) {
        
        if (self.count == 0) {
            
            [subscriber sendCompleted];
        } else {
            @strongify(self);
            [[self.rac_sequence.signal deliverOn:[RACScheduler mainThreadScheduler]] subscribeNext:^(id x) {
                
                ++ index;
                [subscriber sendNext:@{
                                       @(index) : x,
                                       }];
                
            } completed:^{
                [subscriber sendCompleted];
                index = -1;
            }];
        }
        
        return [RACDisposable disposableWithBlock:^{
            
        }];
    }];
}

@end
